<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习6</title>
</head>

<body>

</body>
<script>
    // 1.将一个字符串数组输出为|分割的形式，比如“刘备|张飞|关羽”。使用两种方式实现
    let arr = ["刘备", "张飞", "关羽"];
    let result = arr.join("|");
    console.log(result);

    let arr1 = ["刘备", "张飞", "关羽"];
    let result1 = arr1.reduce((pre, cur) => pre + "|" + cur);
    console.log(result1);

    // 2.将一个字符串数组的元素的顺序进行反转。["a", "b", "c", "d"] -> [ "d","c","b","a"]。使用两种种方式实现。
    let arr2 = ["a", "b", "c", "d"];
    let revArr = arr2.reverse();
    console.log(revArr);

    let arr3 = ["a", "b", "c", "d"];
    let reverseArr = [];
    for (let i = 0; i < arr3.length; i++) {
        reverseArr.unshift(arr3[i]);
    }
    console.log(reverseArr);

    // 3.工资的数组[1500, 1200, 2000, 2100, 1800],把工资超过2000的删除
    let salary = [1500, 1200, 2000, 2100, 1800];
    let filter = salary.filter(salary => salary <= 2000);
    console.log(filter);

    // 4.["c", "a", "z", "a", "x", "a"]找到数组中每一个a出现的位置
    let arr4 = ["c", "a", "z", "a", "x", "a"];
    let position = [];
    arr4.forEach((item, index) => {
        if (item === "a") {
            position.push(index);
        }
    });
    console.log(position);

    // 5.编写一个方法去掉一个数组的重复元素
    function unique(arr) {
        return arr.filter((item, index) => arr.indexOf(item) == index);
    }
    console.log(unique([1, 2, 3, 2, 4, 1]));
</script>

</html>